# =====================================================# =====================================================

# RSS Reader AI - API 测试脚本 (PowerShell)# RSS Reader AI - API 测试脚本 (PowerShell)

# =====================================================# =====================================================



$baseUrl = "http://localhost:8080"$baseUrl = "http://localhost:8080"



Write-Host "🚀 开始测试 RSS Reader AI 后端 API" -ForegroundColor GreenWrite-Host "🚀 开始测试 RSS Reader AI 后端 API" -ForegroundColor Green

Write-Host ("=" * 60)Write-Host "=" * 60



# =====================================================# =====================================================

# 1. 健康检查# 1. 健康检查

# =====================================================# =====================================================

Write-Host "`n📋 测试 1: 健康检查接口" -ForegroundColor CyanWrite-Host "`n📋 测试 1: 健康检查接口" -ForegroundColor Cyan

try {try {

    $response = Invoke-RestMethod -Uri "$baseUrl/health" -Method GET    $response = Invoke-RestMethod -Uri "$baseUrl/health" -Method GET

    Write-Host "✅ 健康检查成功" -ForegroundColor Green    Write-Host "✅ 健康检查成功" -ForegroundColor Green

    Write-Host "响应: $($response | ConvertTo-Json -Depth 10)" -ForegroundColor Gray    Write-Host "响应: $($response | ConvertTo-Json -Depth 10)" -ForegroundColor Gray

}} catch {

catch {    Write-Host "❌ 健康检查失败: $_" -ForegroundColor Red

    Write-Host "❌ 健康检查失败: $_" -ForegroundColor Red}

}

# =====================================================

# =====================================================# 2. 获取订阅源列表

# 2. 获取订阅源列表# =====================================================

# =====================================================Write-Host "`n📋 测试 2: 获取订阅源列表" -ForegroundColor Cyan

Write-Host "`n📋 测试 2: 获取订阅源列表" -ForegroundColor Cyantry {

try {    $response = Invoke-RestMethod -Uri "$baseUrl/api/feeds" -Method GET

    $response = Invoke-RestMethod -Uri "$baseUrl/api/feeds" -Method GET    Write-Host "✅ 获取订阅源列表成功" -ForegroundColor Green

    Write-Host "✅ 获取订阅源列表成功" -ForegroundColor Green    Write-Host "订阅源数量: $($response.data.Count)" -ForegroundColor Gray

    Write-Host "订阅源数量: $($response.data.Count)" -ForegroundColor Gray    if ($response.data.Count -gt 0) {

    if ($response.data.Count -gt 0) {        Write-Host "第一个订阅源: $($response.data[0].title)" -ForegroundColor Gray

        Write-Host "第一个订阅源: $($response.data[0].title)" -ForegroundColor Gray    }

    }} catch {

}    Write-Host "❌ 获取订阅源列表失败: $_" -ForegroundColor Red

catch {}

    Write-Host "❌ 获取订阅源列表失败: $_" -ForegroundColor Red

}# =====================================================

# 3. 添加新订阅源

# =====================================================# =====================================================

# 3. 添加新订阅源Write-Host "`n📋 测试 3: 添加新订阅源" -ForegroundColor Cyan

# =====================================================$newFeed = @{

Write-Host "`n📋 测试 3: 添加新订阅源" -ForegroundColor Cyan    url = "https://www.ruanyifeng.com/blog/atom.xml"

$newFeed = @{    title = "阮一峰的网络日志"

    url = "https://www.ruanyifeng.com/blog/atom.xml"    category = "技术博客"

    title = "阮一峰的网络日志"} | ConvertTo-Json

    category = "技术博客"

} | ConvertTo-Jsontry {

    $response = Invoke-RestMethod -Uri "$baseUrl/api/feeds" `

try {        -Method POST `

    $response = Invoke-RestMethod -Uri "$baseUrl/api/feeds" -Method POST -ContentType "application/json; charset=utf-8" -Body $newFeed        -ContentType "application/json; charset=utf-8" `

            -Body $newFeed

    Write-Host "✅ 添加订阅源成功" -ForegroundColor Green    

    $feedId = $response.data.id    Write-Host "✅ 添加订阅源成功" -ForegroundColor Green

    Write-Host "订阅源 ID: $feedId" -ForegroundColor Gray    $feedId = $response.data.id

    Write-Host "订阅源标题: $($response.data.title)" -ForegroundColor Gray    Write-Host "订阅源 ID: $feedId" -ForegroundColor Gray

        Write-Host "订阅源标题: $($response.data.title)" -ForegroundColor Gray

    # 保存 ID 用于后续测试    

    $global:testFeedId = $feedId    # 保存 ID 用于后续测试

}    $global:testFeedId = $feedId

catch {} catch {

    Write-Host "⚠️  添加订阅源失败(可能已存在): $_" -ForegroundColor Yellow    Write-Host "⚠️  添加订阅源失败(可能已存在): $_" -ForegroundColor Yellow

}}



# =====================================================# =====================================================

# 4. 获取订阅源详情# 4. 获取订阅源详情

# =====================================================# =====================================================

Write-Host "`n📋 测试 4: 获取订阅源详情" -ForegroundColor CyanWrite-Host "`n📋 测试 4: 获取订阅源详情" -ForegroundColor Cyan

if ($global:testFeedId) {if ($global:testFeedId) {

    try {    try {

        $response = Invoke-RestMethod -Uri "$baseUrl/api/feeds/$global:testFeedId" -Method GET        $response = Invoke-RestMethod -Uri "$baseUrl/api/feeds/$global:testFeedId" -Method GET

        Write-Host "✅ 获取订阅源详情成功" -ForegroundColor Green        Write-Host "✅ 获取订阅源详情成功" -ForegroundColor Green

        Write-Host "标题: $($response.data.title)" -ForegroundColor Gray        Write-Host "标题: $($response.data.title)" -ForegroundColor Gray

        Write-Host "URL: $($response.data.url)" -ForegroundColor Gray        Write-Host "URL: $($response.data.url)" -ForegroundColor Gray

        Write-Host "分类: $($response.data.category)" -ForegroundColor Gray        Write-Host "分类: $($response.data.category)" -ForegroundColor Gray

    }    } catch {

    catch {        Write-Host "❌ 获取订阅源详情失败: $_" -ForegroundColor Red

        Write-Host "❌ 获取订阅源详情失败: $_" -ForegroundColor Red    }

    }} else {

}    Write-Host "⚠️  跳过测试(没有可用的订阅源 ID)" -ForegroundColor Yellow

else {}

    Write-Host "⚠️  跳过测试(没有可用的订阅源 ID)" -ForegroundColor Yellow

}# =====================================================

# 5. 手动刷新订阅源

# =====================================================# =====================================================

# 5. 手动刷新订阅源Write-Host "`n📋 测试 5: 手动刷新订阅源(抓取文章)" -ForegroundColor Cyan

# =====================================================if ($global:testFeedId) {

Write-Host "`n📋 测试 5: 手动刷新订阅源(抓取文章)" -ForegroundColor Cyan    try {

if ($global:testFeedId) {        $response = Invoke-RestMethod -Uri "$baseUrl/api/feeds/$global:testFeedId/refresh" -Method POST

    try {        Write-Host "✅ 刷新任务已提交" -ForegroundColor Green

        $response = Invoke-RestMethod -Uri "$baseUrl/api/feeds/$global:testFeedId/refresh" -Method POST        Write-Host "响应: $($response.message)" -ForegroundColor Gray

        Write-Host "✅ 刷新任务已提交" -ForegroundColor Green        

        Write-Host "响应: $($response.message)" -ForegroundColor Gray        # 等待抓取完成

                Write-Host "⏳ 等待 3 秒..." -ForegroundColor Gray

        # 等待抓取完成        Start-Sleep -Seconds 3

        Write-Host "⏳ 等待 3 秒..." -ForegroundColor Gray    } catch {

        Start-Sleep -Seconds 3        Write-Host "❌ 刷新订阅源失败: $_" -ForegroundColor Red

    }    }

    catch {} else {

        Write-Host "❌ 刷新订阅源失败: $_" -ForegroundColor Red    Write-Host "⚠️  跳过测试(没有可用的订阅源 ID)" -ForegroundColor Yellow

    }}

}

else {# =====================================================

    Write-Host "⚠️  跳过测试(没有可用的订阅源 ID)" -ForegroundColor Yellow# 6. 获取文章列表

}# =====================================================

Write-Host "`n📋 测试 6: 获取文章列表" -ForegroundColor Cyan

# =====================================================try {

# 6. 获取文章列表    $response = Invoke-RestMethod -Uri "$baseUrl/api/articles?page=1&size=10" -Method GET

# =====================================================    Write-Host "✅ 获取文章列表成功" -ForegroundColor Green

Write-Host "`n📋 测试 6: 获取文章列表" -ForegroundColor Cyan    Write-Host "总文章数: $($response.data.total)" -ForegroundColor Gray

try {    Write-Host "当前页文章数: $($response.data.articles.Count)" -ForegroundColor Gray

    $url = "$baseUrl/api/articles?page=1&size=10"    

    $response = Invoke-RestMethod -Uri $url -Method GET    if ($response.data.articles.Count -gt 0) {

    Write-Host "✅ 获取文章列表成功" -ForegroundColor Green        $article = $response.data.articles[0]

    Write-Host "总文章数: $($response.data.total)" -ForegroundColor Gray        Write-Host "第一篇文章标题: $($article.title)" -ForegroundColor Gray

    Write-Host "当前页文章数: $($response.data.articles.Count)" -ForegroundColor Gray        

            # 保存文章 ID 用于后续测试

    if ($response.data.articles.Count -gt 0) {        $global:testArticleId = $article.id

        $article = $response.data.articles[0]    }

        Write-Host "第一篇文章标题: $($article.title)" -ForegroundColor Gray} catch {

            Write-Host "❌ 获取文章列表失败: $_" -ForegroundColor Red

        # 保存文章 ID 用于后续测试}

        $global:testArticleId = $article.id

    }# =====================================================

}# 7. 获取文章详情

catch {# =====================================================

    Write-Host "❌ 获取文章列表失败: $_" -ForegroundColor RedWrite-Host "`n📋 测试 7: 获取文章详情" -ForegroundColor Cyan

}if ($global:testArticleId) {

    try {

# =====================================================        $response = Invoke-RestMethod -Uri "$baseUrl/api/articles/$global:testArticleId" -Method GET

# 7. 获取文章详情        Write-Host "✅ 获取文章详情成功" -ForegroundColor Green

# =====================================================        Write-Host "标题: $($response.data.title)" -ForegroundColor Gray

Write-Host "`n📋 测试 7: 获取文章详情" -ForegroundColor Cyan        Write-Host "作者: $($response.data.author)" -ForegroundColor Gray

if ($global:testArticleId) {        Write-Host "发布时间: $($response.data.published_at)" -ForegroundColor Gray

    try {    } catch {

        $response = Invoke-RestMethod -Uri "$baseUrl/api/articles/$global:testArticleId" -Method GET        Write-Host "❌ 获取文章详情失败: $_" -ForegroundColor Red

        Write-Host "✅ 获取文章详情成功" -ForegroundColor Green    }

        Write-Host "标题: $($response.data.title)" -ForegroundColor Gray} else {

        Write-Host "作者: $($response.data.author)" -ForegroundColor Gray    Write-Host "⚠️  跳过测试(没有可用的文章 ID)" -ForegroundColor Yellow

        Write-Host "发布时间: $($response.data.published_at)" -ForegroundColor Gray}

    }

    catch {# =====================================================

        Write-Host "❌ 获取文章详情失败: $_" -ForegroundColor Red# 8. 标记文章为已读

    }# =====================================================

}Write-Host "`n📋 测试 8: 标记文章为已读" -ForegroundColor Cyan

else {if ($global:testArticleId) {

    Write-Host "⚠️  跳过测试(没有可用的文章 ID)" -ForegroundColor Yellow    try {

}        $response = Invoke-RestMethod -Uri "$baseUrl/api/articles/$global:testArticleId/read" -Method POST

        Write-Host "✅ 标记为已读成功" -ForegroundColor Green

# =====================================================    } catch {

# 8. 标记文章为已读        Write-Host "❌ 标记为已读失败: $_" -ForegroundColor Red

# =====================================================    }

Write-Host "`n📋 测试 8: 标记文章为已读" -ForegroundColor Cyan} else {

if ($global:testArticleId) {    Write-Host "⚠️  跳过测试(没有可用的文章 ID)" -ForegroundColor Yellow

    try {}

        $response = Invoke-RestMethod -Uri "$baseUrl/api/articles/$global:testArticleId/read" -Method POST

        Write-Host "✅ 标记为已读成功" -ForegroundColor Green# =====================================================

    }# 9. 收藏文章

    catch {# =====================================================

        Write-Host "❌ 标记为已读失败: $_" -ForegroundColor RedWrite-Host "`n📋 测试 9: 收藏文章" -ForegroundColor Cyan

    }if ($global:testArticleId) {

}    try {

else {        $response = Invoke-RestMethod -Uri "$baseUrl/api/articles/$global:testArticleId/favorite" -Method POST

    Write-Host "⚠️  跳过测试(没有可用的文章 ID)" -ForegroundColor Yellow        Write-Host "✅ 收藏文章成功" -ForegroundColor Green

}    } catch {

        Write-Host "❌ 收藏文章失败: $_" -ForegroundColor Red

# =====================================================    }

# 9. 收藏文章} else {

# =====================================================    Write-Host "⚠️  跳过测试(没有可用的文章 ID)" -ForegroundColor Yellow

Write-Host "`n📋 测试 9: 收藏文章" -ForegroundColor Cyan}

if ($global:testArticleId) {

    try {# =====================================================

        $response = Invoke-RestMethod -Uri "$baseUrl/api/articles/$global:testArticleId/favorite" -Method POST# 10. 搜索文章

        Write-Host "✅ 收藏文章成功" -ForegroundColor Green# =====================================================

    }Write-Host "`n📋 测试 10: 搜索文章" -ForegroundColor Cyan

    catch {try {

        Write-Host "❌ 收藏文章失败: $_" -ForegroundColor Red    $response = Invoke-RestMethod -Uri "$baseUrl/api/articles/search?q=技术" -Method GET

    }    Write-Host "✅ 搜索文章成功" -ForegroundColor Green

}    Write-Host "搜索结果数量: $($response.data.Count)" -ForegroundColor Gray

else {} catch {

    Write-Host "⚠️  跳过测试(没有可用的文章 ID)" -ForegroundColor Yellow    Write-Host "❌ 搜索文章失败: $_" -ForegroundColor Red

}}



# =====================================================# =====================================================

# 10. 搜索文章# 11. 获取统计信息

# =====================================================# =====================================================

Write-Host "`n📋 测试 10: 搜索文章" -ForegroundColor CyanWrite-Host "`n📋 测试 11: 获取订阅源统计信息" -ForegroundColor Cyan

try {try {

    $url = "$baseUrl/api/articles/search?q=技术"    $response = Invoke-RestMethod -Uri "$baseUrl/api/feeds/stats" -Method GET

    $response = Invoke-RestMethod -Uri $url -Method GET    Write-Host "✅ 获取统计信息成功" -ForegroundColor Green

    Write-Host "✅ 搜索文章成功" -ForegroundColor Green    Write-Host "总订阅源数: $($response.data.total)" -ForegroundColor Gray

    Write-Host "搜索结果数量: $($response.data.Count)" -ForegroundColor Gray    Write-Host "启用的订阅源: $($response.data.active)" -ForegroundColor Gray

}} catch {

catch {    Write-Host "❌ 获取统计信息失败: $_" -ForegroundColor Red

    Write-Host "❌ 搜索文章失败: $_" -ForegroundColor Red}

}

Write-Host "`n📋 测试 12: 获取文章统计信息" -ForegroundColor Cyan

# =====================================================try {

# 11. 获取统计信息    $response = Invoke-RestMethod -Uri "$baseUrl/api/articles/stats" -Method GET

# =====================================================    Write-Host "✅ 获取文章统计信息成功" -ForegroundColor Green

Write-Host "`n📋 测试 11: 获取订阅源统计信息" -ForegroundColor Cyan    Write-Host "总文章数: $($response.data.total)" -ForegroundColor Gray

try {    Write-Host "未读文章: $($response.data.unread)" -ForegroundColor Gray

    $response = Invoke-RestMethod -Uri "$baseUrl/api/feeds/stats" -Method GET    Write-Host "收藏文章: $($response.data.favorite)" -ForegroundColor Gray

    Write-Host "✅ 获取统计信息成功" -ForegroundColor Green} catch {

    Write-Host "总订阅源数: $($response.data.total)" -ForegroundColor Gray    Write-Host "❌ 获取文章统计信息失败: $_" -ForegroundColor Red

    Write-Host "启用的订阅源: $($response.data.active)" -ForegroundColor Gray}

}

catch {# =====================================================

    Write-Host "❌ 获取统计信息失败: $_" -ForegroundColor Red# 总结

}# =====================================================

Write-Host "`n" + "=" * 60

Write-Host "`n📋 测试 12: 获取文章统计信息" -ForegroundColor CyanWrite-Host "🎉 API 测试完成!" -ForegroundColor Green

try {Write-Host "`n💡 提示:" -ForegroundColor Yellow

    $response = Invoke-RestMethod -Uri "$baseUrl/api/articles/stats" -Method GETWrite-Host "  - 如需测试 AI 摘要功能,请先配置 config.yaml 中的 Coze API 密钥" -ForegroundColor Gray

    Write-Host "✅ 获取文章统计信息成功" -ForegroundColor GreenWrite-Host "  - 完整的 API 文档请查看: backend/internal/handler/README.md" -ForegroundColor Gray

    Write-Host "总文章数: $($response.data.total)" -ForegroundColor GrayWrite-Host "  - 可以使用 Postman 导入 API 文档进行更详细的测试" -ForegroundColor Gray

    Write-Host "未读文章: $($response.data.unread)" -ForegroundColor Gray
    Write-Host "收藏文章: $($response.data.favorite)" -ForegroundColor Gray
}
catch {
    Write-Host "❌ 获取文章统计信息失败: $_" -ForegroundColor Red
}

# =====================================================
# 总结
# =====================================================
Write-Host "`n" -NoNewline
Write-Host ("=" * 60)
Write-Host "🎉 API 测试完成!" -ForegroundColor Green
Write-Host "`n💡 提示:" -ForegroundColor Yellow
Write-Host "  - 如需测试 AI 摘要功能,请先配置 config.yaml 中的 Coze API 密钥" -ForegroundColor Gray
Write-Host "  - 完整的 API 文档请查看: backend/internal/handler/README.md" -ForegroundColor Gray
Write-Host "  - 可以使用 Postman 导入 API 文档进行更详细的测试" -ForegroundColor Gray
